Page({
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    userInfo: {},
    hasUserInfo: false,
    canIUseGetUserProfile: false,
    cache: {}
  },
  onLoad() {
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
  },
  getUserProfile(e) {
    wx.login({
      success: res => {
        if (res.code) {
          wx.request({
            url: 'http://localhost/WxLogin',
            method: "get",
            data: {
              code: res.code
            },
            success(res) {
              wx.setStorageSync('openid', res.data.data.openid)
              wx.setStorageSync('wxKey', res.data.data.wxKey)
            }
          })
        }
      }
    })
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        wx.request({
          url: 'http://localhost/WxInfo/' + wx.getStorageSync('openid'),
          method: "post",
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          data: {
            nickname: res.userInfo.nickName,
            avatarUrl: res.userInfo.avatarUrl
          },
          success(res) {
            console.log("WxInfo接口")
            console.log(res)
            wx.setStorageSync('role', res.data.data.Role)
            wx.request({
              url: 'http://localhost/Wx/Userinfo?openid=' + wx.getStorageSync('openid'),
              method: "POST",
              success(res) {
                console.log(res.data)
                wx.setStorageSync('userDetail', res.data.data)
              }

            })
            wx.switchTab({
              url: '/pages/index/index',
            })
          }
        })
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true,
        })
      }
    })
  }
})